package com.kjsdfgk.hjmdsf.livev3.mvp.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.kjsdfgk.hjmdsf.ftlive.mvp.ui.fragment.LiveV3FtFragment;
import com.kjsdfgk.hjmdsf.ftmine.mvp.ui.fragment.MineV3FtFragment;
import com.kjsdfgk.hjmdsf.ftmzt.mvp.ui.fragment.MeiZhiTuFragment;
import com.kjsdfgk.hjmdsf.ftvideo.mvp.ui.fragment.VideoV3FtFragment;
import com.kjsdfgk.hjmdsf.livev3.di.component.DaggerLiveV3Component;
import com.kjsdfgk.hjmdsf.livev3.mvp.contract.LiveV3Contract;
import com.kjsdfgk.hjmdsf.livev3.mvp.presenter.LiveV3Presenter;
import com.lksdkjs.live.R;
import com.tbruyelle.rxpermissions2.RxPermissions;


import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/27/2020 23:28
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class LiveV3Activity extends BaseActivity<LiveV3Presenter> implements LiveV3Contract.View {

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLiveV3Component //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_live_v3; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }
    private BaseFragment[] mFragments = new BaseFragment[4];
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOURTH = 3;

    RxPermissions mRxPermissions;
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRxPermissions = new RxPermissions(this);

        mFragments[FIRST] = LiveV3FtFragment.newInstance();
        mFragments[SECOND] = VideoV3FtFragment.newInstance();
        mFragments[THIRD] = MeiZhiTuFragment.newInstance();
        mFragments[FOURTH] = MineV3FtFragment.newInstance();

        loadMultipleRootFragment(R.id.fragment_biz_container, FIRST,
                mFragments[FIRST],
                mFragments[SECOND],
                mFragments[THIRD],
                mFragments[FOURTH]);

        initBottomNavigationBar(0);



        mPresenter.generateDeviceUser( );

        mPresenter.getKjhfsData( );
    }

    @Override
    public void showLoading(String key) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public boolean isCancel() {
        return false;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    int index = 0;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    private void initBottomNavigationBar(@IntRange(from = 0, to = 3) int position) {
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.img_home , "直播"));
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.img_video, "视频"));
       bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.img_photo, "图片"));
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.img_my, "我的"));

        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        bottomNavigationBar.setFirstSelectedPosition(position);
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.SimpleOnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                showHideFragment(mFragments[position], mFragments[index]);
                index = position;
            }
        });
        bottomNavigationBar.initialise();
    }



    private boolean isOnKeyBacking = false;
    private final Handler mainLoopHandler = new Handler(Looper.getMainLooper());
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isOnKeyBacking) {
                mainLoopHandler.removeCallbacks(onBackTimeThread);
                isOnKeyBacking = false;
                finish();
            }else {
                isOnKeyBacking = true;
                mainLoopHandler.postDelayed(onBackTimeThread, 2000);
                showMessage("再按一下退出");

            }
            return true;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }
    private final Runnable onBackTimeThread = new Runnable(){
        public void run() {
            isOnKeyBacking = false;
        }
    };

    @Override
    public RxPermissions getRxPermissions() {
        return mRxPermissions;
    }


}
